package com.nofeng.wx.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

public class AIUtils {

    public static String aiForApp(String ask,String applyId,String token){
        try {
            Map<String, String> headerMap = new HashMap<>();
            headerMap.put("Authorization", "Bearer "+token);
            headerMap.put("X-Source", "openapi");
            headerMap.put("Content-Type", "application/json");

            // 构建最外层 JSONObject
            JSONObject jsonObject = new JSONObject();
            // 设置基础字段
            jsonObject.put("assistant_id", applyId);
            jsonObject.put("user_id", "1983471508573869120"); // 替换为实际用户ID
            jsonObject.put("stream", false);

            // 构建 messages 数组（List 中的 JSONObject）
            JSONObject message = new JSONObject();
            message.put("role", "user");

            // 构建 content 数组（List 中的 JSONObject）
            JSONObject content = new JSONObject();
            content.put("type", "text");
            content.put("text", ask);
            JSONArray objects = new JSONArray();
            objects.add(content);
            message.put("content", objects); // 将 content 添加到 content 数组
            jsonObject.put("messages", Lists.newArrayList(message)); // 将 message 添加到 messages 数组

            RestTemplate restTemplate = NoHttpsClientUtils.getRestTemplate();
            HttpHeaders headers = new HttpHeaders();
            headerMap.forEach(headers::add);
            org.springframework.http.HttpEntity<String> entity = new HttpEntity<>(jsonObject.toJSONString(), headers);
            String url = "https://yuanqi.tencent.com/openapi/v1/agent/chat/completions";
            String resp = restTemplate.postForObject(url, entity, String.class);
            JSONObject resultJSON = JSONObject.parseObject(resp);
            JSONArray jsonArray = resultJSON.getJSONArray("choices");
            for (int i = 0; i < jsonArray.size(); i++) {
                return jsonArray.getJSONObject(i).getJSONObject("message").getString("content");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String ai(String ask){
        try {
            Map<String, String> headerMap = new HashMap<>();
            headerMap.put("Authorization", "Bearer asPTGSgvYY5GnfSpfl4RqpLlhtjguPmJ");
            headerMap.put("X-Source", "openapi");
            headerMap.put("Content-Type", "application/json");

            // 构建最外层 JSONObject
            JSONObject jsonObject = new JSONObject();
            // 设置基础字段
            jsonObject.put("assistant_id", "1983471508573869120");
            jsonObject.put("user_id", "1983471508573869120"); // 替换为实际用户ID
            jsonObject.put("stream", false);

            // 构建 messages 数组（List 中的 JSONObject）
            JSONObject message = new JSONObject();
            message.put("role", "user");

            // 构建 content 数组（List 中的 JSONObject）
            JSONObject content = new JSONObject();
            content.put("type", "text");
            content.put("text", ask);
            JSONArray objects = new JSONArray();
            objects.add(content);
            message.put("content", objects); // 将 content 添加到 content 数组
            jsonObject.put("messages", Lists.newArrayList(message)); // 将 message 添加到 messages 数组

            RestTemplate restTemplate = NoHttpsClientUtils.getRestTemplate();
            HttpHeaders headers = new HttpHeaders();
            headerMap.forEach(headers::add);
            org.springframework.http.HttpEntity<String> entity = new HttpEntity<>(jsonObject.toJSONString(), headers);
            String url = "https://yuanqi.tencent.com/openapi/v1/agent/chat/completions";
            String resp = restTemplate.postForObject(url, entity, String.class);
            JSONObject resultJSON = JSONObject.parseObject(resp);
            JSONArray jsonArray = resultJSON.getJSONArray("choices");
            for (int i = 0; i < jsonArray.size(); i++) {
                return jsonArray.getJSONObject(i).getJSONObject("message").getString("content");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
